<?php
namespace app\index\controller;

use think\Request;
use think\Db;
use app\index\model\answer as AnswerModel;
use app\index\model\question as QuestionModel;
use app\index\model\noticeQuestion as NoticeQuestion;

class Mine
{
    public function getMyAskQuestion(Request $request)
    {
        //获取请求参数
        $userToken = $request->param("userToken");
        $lastId = $request->param("lastId");
        $questionNum = $request->param("questionNum");
       
        //查询
        $query = $lastId == 0 ? 
        function ($query) use ($userToken, $lastId, $questionNum){
            $query->where("askerToken", $userToken)->order('id', 'desc')->limit($questionNum);
        } :
        function ($query) use ($userToken, $lastId, $questionNum) {
            $query->where("askerToken", $userToken)->where('id', '<', $lastId)->order('id', 'desc')->limit($questionNum);
        };
        $allNote = QuestionModel::all($query);
        
        //查询相关数据
        $Question = controller("index/Question");
        $allNote = $Question->findQuestionAboutInfo($allNote, $userToken);
        
      
        //返回数据
        $JsonData = controller("index/JsonData");
        $json = $JsonData->getJsonData(0, "get ask question success.", $allNote);
        return $json;

    }
    public function getMyAnswerQuestion(Request $request)
    {
        //获取请求参数
        $userToken = $request->param("userToken");
        $lastId = $request->param("lastId");
        $questionNum = $request->param("questionNum");

        //定义sql语句
        $questionTab = "dcw_fastask_question";
        $answerTab = "dcw_fastask_answer";
        $sql = "";
        if ($lastId == 0) {
            $sql = "SELECT 
                    $questionTab.*,
                    $answerTab.answerType,
                    $answerTab.answerContent,
                    $answerTab.answerTime
                    FROM $answerTab, $questionTab
                    WHERE $answerTab.answererToken='$userToken'
                    AND $answerTab.questionToken=$questionTab.id
                    ORDER BY $questionTab.id DESC
                    LIMIT $questionNum
                    ";
        }
        else {
            $sql = "SELECT 
                    $questionTab.*,
                    $answerTab.answerType,
                    $answerTab.answerContent,
                    $answerTab.answerTime
                    FROM $answerTab, $questionTab
                    WHERE $answerTab.answererToken='$userToken'
                    AND $answerTab.questionToken=$questionTab.id
                    AND $questionTab.id < $lastId
                    ORDER BY $questionTab.id DESC
                    LIMIT $questionNum
                    ";
        }
        //执行sql，获取数据
        $allNote = Db::query($sql);

        //查询问题的相关数据
        $Question = controller("index/Question");
        $allNote = $Question->findQuestionAboutInfo($allNote, $userToken);
        
        //返回消息
        $JsonData = controller("index/JsonData");
        $json = $JsonData->getJsonData(0, "get answer question success.", $allNote);
        return $json;
    }
    public function getQuestionAskMe(Request $request)
    {
        //获取请求参数
        $userToken = $request->param("userToken");
        $lastId = $request->param("lastId");
        $questionNum = $request->param("questionNum");

        //查询数据
        $query = $lastId == 0 ? 
        function ($query) use ($userToken, $lastId, $questionNum){
            $query->where("expertToken", $userToken)->order('id', 'desc')->limit($questionNum);
        } :
        function ($query) use ($userToken, $lastId, $questionNum) {
            $query->where("expertToken", $userToken)->where('id', '<', $lastId)->order('id', 'desc')->limit($questionNum);
        };
        $allNote = QuestionModel::all($query);
        
        //查询问题相关数据
        $Question = controller("index/Question");
        $allNote = $Question->findQuestionAboutInfo($allNote, $userToken);

        //返回消息
        $JsonData = controller("index/JsonData");
        $json = $JsonData->getJsonData(0, "get ask question success.", $allNote);
        return $json;
    }
    public function getMyNoticeQuestion(Request $request)
    {
        //获取请求参数
        $userToken = $request->param("userToken");
        $lastId = $request->param("lastId");
        $questionNum = $request->param("questionNum");

        //定义sql语句
        $questionTab = "dcw_fastask_question";
        $noticeQuestionTab = "dcw_fastask_noticeQuestion";
        $sql = "";
        if ($lastId == 0) {
            $sql = "SELECT $questionTab.*,
                    $noticeQuestionTab.timestamp AS whenNotice
                    FROM $questionTab, $noticeQuestionTab
                    WHERE $noticeQuestionTab.userToken='$userToken'
                    AND $noticeQuestionTab.questionToken=$questionTab.id
                    ORDER BY $questionTab.id DESC 
                    LIMIT $questionNum
            ";
        }
        else {
            $sql = "SELECT $questionTab.*,
                    $noticeQuestionTab.timestamp AS whenNotice
                    FROM $questionTab, $noticeQuestionTab
                    WHERE $noticeQuestionTab.userToken='$userToken'
                    AND $noticeQuestionTab.questionToken=$questionTab.id
                    AND $questionTab.id<$lastId
                    ORDER BY $questionTab.id DESC 
                    LIMIT $questionNum
            ";
        }
        //执行sql
        $allNote = Db::query($sql);

        //查询相关字段数据
        $Question = controller("index/Question");
        $allNote = $Question->findQuestionAboutInfo($allNote, $userToken);
       
        //返回消息
        $JsonData = controller("index/JsonData");
        $json = $JsonData->getJsonData(0, "get notice question success.", $allNote);
        return $json;

    }
    
    public function getAboutQuestionNum(Request $request)
    {
        //获取请求参数
        $userToken = $request->param("userToken");
        $userToken = $userToken == null ? " " : $userToken;
        
        //构造sql语句
        $questionTab = "dcw_fastask_question";
        $answerTab = "dcw_fastask_answer";
        $noticeTab = "dcw_fastask_noticeQuestion";
        
        $sql = "SELECT
        (SELECT COUNT(*) FROM $questionTab WHERE askerToken='$userToken') As myAskQuestionNum,
        (SELECT COUNT(*) FROM $answerTab WHERE answererToken='$userToken') AS myAnswerNum,
        (SELECT COUNT(*) FROM $questionTab WHERE expertToken='$userToken' AND expertToken != ' ') AS askMeQuestionNum,
        (SELECT COUNT(*) FROM $noticeTab WHERE userToken='$userToken') AS myNoticeQuestionNum
        ";
        //查询
        $findRes = Db::query($sql);
        //返回消息
        $JsonData = controller("index/JsonData");
        $json = $JsonData->getJsonData(0, "get question about success.", $findRes);
        return $json;
    }

    public function getAboutQuestionNumNoReq($userToken)
    {
   
        $userToken = $userToken == null ? " " : $userToken;
        
        //构造sql语句
        $questionTab = "dcw_fastask_question";
        $answerTab = "dcw_fastask_answer";
        $noticeTab = "dcw_fastask_noticeQuestion";
        
        $sql = "SELECT
        (SELECT COUNT(*) FROM $questionTab WHERE askerToken='$userToken') As myAskQuestionNum,
        (SELECT COUNT(*) FROM $answerTab WHERE answererToken='$userToken') AS myAnswerNum,
        (SELECT COUNT(*) FROM $questionTab WHERE expertToken='$userToken' AND expertToken != ' ') AS askMeQuestionNum,
        (SELECT COUNT(*) FROM $noticeTab WHERE userToken='$userToken') AS myNoticeQuestionNum
        ";
        //查询
        $findRes = Db::query($sql);
        return $findRes;
    }
}